package com.buba.controller;

import com.buba.pojo.Fruit;
import com.buba.pojo.Page;
import com.buba.service.FruitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Fruit")
public class FruitController {
    private FruitService fruitService;

    /**
     * 查询
     * @return
     * @RequestParam 接收页面参数，并封装成map
     * 也可以直接传参数selFruit(long pageNow){}
     */
    @RequestMapping("/selFruit")
    public Page<Fruit> selFruit(@RequestParam Map<String,Object> map){
        Page<Fruit> page = new Page<>(Long.parseLong(map.get("pageNow").toString()),Long.parseLong(map.get("pageCount").toString()));
        map.put("page",page);
        //或者存到page类的SetconditionMap   page.SetconditionMap(map);
        page.setTotalCount(fruitService.selCount(map));
        page.setList(fruitService.selFruit(map));
        return page;
    }

    /**
     * 添加
     * @param
     * @return
     */
    @RequestMapping("/addFruit")
    public Map<String,Object> addFruit(@RequestParam Map<String,Object> map){
        Integer addCount = fruitService.addFruit(map);
        if(addCount==1){
            map.put("message","添加成功");
        }
        return map;
    }
    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping("/delFruit")
    public Integer delFruit(String ids){
        Integer delCount = fruitService.delFruit(ids);
        return delCount;
    }

    /**
     * 修改
     * @param fruit
     * @return
     */
    @RequestMapping("/updFruit")
    public Integer updFruit(Fruit fruit){
        Integer updCount = fruitService.updFruit(fruit);
        return updCount;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("/allDelFruit")
    public Integer allDelFruit(String ids){
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);//该方法是将数组转化成List集合
        Integer allDelCount = fruitService.allDelFruit(list);
        return allDelCount;
    }


    @Autowired
    public void setFruitService(FruitService fruitService) {
        this.fruitService = fruitService;
    }
}
